package com.zy.hospitalward_management_system.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.hospitalward_management_system.pojo.Department;
import com.zy.hospitalward_management_system.pojo.Inpatientward;
import com.zy.hospitalward_management_system.service.impl.DepartmentServiceImpl;
import com.zy.hospitalward_management_system.service.impl.InpatientwardServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zy
 * @since 2021-01-26
 */
@Controller
public class DepartmentController {

    @Resource
    DepartmentServiceImpl departmentService;

    @Resource
    InpatientwardServiceImpl inpatientwardService;

    /**
     * 传入科室ID 跳转到科室Info页面
     * @param id 科室ID
     * @param model
     * @return
     */
    @GetMapping("/toDepartmentInfo/{id}")
    public String toDepartmentInfo(@PathVariable("id") int id, Model model){
        Department department = departmentService.getBaseMapper().selectById(id);
        model.addAttribute("department",department);
        return "user/info/department_info";
    }

    @GetMapping("/toDepartmentAdd")
    public String toDepartmentAdd(){
        return "user/add/department_add";
    }


    /**
     * 添加科室
     * @param department 科室对象
     * @return 返回信息
     */
    @PostMapping("/department/add")
    @ResponseBody
    public String addDepartment(Department department){

        if(department.getDepartmentName().isEmpty())
            return "科室名不能为空!";

        if(department.getDepartmentPhone().isEmpty())
            return "科室联系方式不能为空!";

        if(department.getDepartmentAddress().isEmpty())
            return "科室地址不能为空!";

        Department departmentEx = departmentService.getBaseMapper().selectOne(new QueryWrapper<Department>().eq("department_name", department.getDepartmentName()));
        if(departmentEx!=null){
            return "该科室已经存在!";
        }

        int insert = departmentService.getBaseMapper().insert(department);
        if(insert==1){
            return "添加成功!";
        }else {
            return "添加失败!";
        }
    }



    /**
     * 传入科室对象 对科室信息进行更新
     * @param department 科室对象
     * @return 更新结果
     */
    @PostMapping("/department/update")
    @ResponseBody
    public String updateDepartmentById(Department department){

        if(department.getDepartmentName().isEmpty())
            return "科室名不能为空!";

        if(department.getDepartmentPhone().isEmpty())
            return "科室联系方式不能为空!";

        if(department.getDepartmentAddress().isEmpty())
            return "科室地址不能为空!";

        int i = departmentService.getBaseMapper().updateById(department);
        if(i==1){
            return "修改成功!";
        }else{
            return "修改失败!";
        }
    }


    @GetMapping("/department/delete/")
    @ResponseBody
    public String deleteDepartmentById(String id){
        System.out.println(id);
        int i = departmentService.getBaseMapper().deleteById(id);
        if(i==1){
            return "删除成功!";
        }else {
            return "删除失败!";
        }
    }


    /**
     * 获取所有科室的名称信息 order by department_id
     * @return List<科室名称>
     */
    @GetMapping("/department/name")
    @ResponseBody
    public String selectDepartmentName(){
        List<Department> departments = departmentService.getBaseMapper().selectList(null);
        List<Map<String,Object>> maps = new ArrayList<>();
        for (Department department: departments) {
            Integer count = inpatientwardService.getBaseMapper().selectCount(new QueryWrapper<Inpatientward>().eq("department_id", department.getDepartmentId()));
            HashMap<String, Object> map = new HashMap<>();
            map.put("department_name", department.getDepartmentName());
            map.put("department_count", count);
            maps.add(map);
        }
        System.out.println(maps);
        return JSON.toJSONString(maps);
    }

}

